package cn.jg.framework.auth.model;

import cn.jg.framework.common.model.SimpleBaseModel;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.google.common.collect.Lists;
import lombok.*;

import java.util.List;
import java.util.Objects;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = false,exclude = "position")
@JsonInclude(value = JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class User extends SimpleBaseModel {
	private static final long serialVersionUID = 1L;

	private Company company;

	private Integer udeskId;

	private Integer positionId;

	private Position position;

	private String username;

	private String password;

    private Integer companyId;

	/**
	 * 密码加密版本
	 */
	private String encryptVersion;

	private String realname;

	private String email;

	private String mobile;

	private Integer type = Type.NORMAL.getValue();

	private Integer status;

	private String headPortraitUrl;

	private String emailToken;

	private String remoteId;

	private List<Integer> roleIdList = Lists.newArrayList();

	private List<Role> roleList = Lists.newArrayList();

	private List<Position> positionList= Lists.newArrayList();

	private List<UserGroup> userGroupList = Lists.newArrayList();

	private List<AuthorizationSet> authorizationSetList = Lists.newArrayList();

	private String remoteType;

	private String language;

	private Integer comUdeskId;

	private Integer deptParentId;

	/**
	 * 知识访问量
	 */
	private Integer viewCount;

	/**
	 * 知识下载量
	 */
	private Integer downloadCount;

    public Integer getPositionId() {
        if (Objects.nonNull(this.position)){
            return this.position.getId();
        }else {
            return null;
        }
    }

    public boolean isSuperAdmin() {
		return null != this.type && this.type.equals(Type.SUPER_ADMIN.getValue());
	}

	public boolean isAdmin() {
		return null != this.type && (
				this.type.equals(Type.ADMIN.getValue())
						|| this.type.equals(Type.SUPER_ADMIN.getValue())
						|| this.type.equals(Type.FAKER.getValue())
		);
	}

	public boolean isNormal() {
		return null != this.type && this.type.equals(Type.NORMAL.getValue());
	}

	public boolean isFaker() {
		return null != this.type && this.type.equals(Type.FAKER.getValue());
	}

	public enum Status {
		DISABLE(0), ENABLE(1);

		private int value;

		Status(int value) {
			this.value = value;
		}

		public int getValue() {
			return value;
		}
	}

	public enum Type {

		SUPER_ADMIN(1, "superAdmin"), ADMIN(2, "admin"), NORMAL(3, "agent"), FAKER(4, "faker");

		private int value;
		private String name;

		Type(int value, String name) {
			this.value = value;
			this.name = name;
		}

		public int getValue() {
			return value;
		}

		public String getName() {
			return name;
		}
	}

	public enum RemoteType {
		UNKNOWN("unknown"),
		LOCAL("local"),
		UDESK("udesk"),
		KM("km"),
		SERVICE_GO("serviceGo"),
		WECOM("weCom");

		@Getter
		private String value;

		RemoteType(String value) {
			this.value = value;
		}

		public static RemoteType of(String remoteTypeString) {
			for (RemoteType value : RemoteType.values()) {
				if (Objects.equals(value.value, remoteTypeString)) {
					return value;
				}
			}

			return UNKNOWN;
		}
	}

	public enum Language {
		SYSTEM("SY-LA"), CHINESE("ZH-CN"), ENGLISH("EN-US"), JAPANESE("JA-JP"),
		;

		@Getter
		private String value;

		Language(String value) {
			this.value = value;
		}
	}

	public enum EncryptVersion {
		SHA1("sha1"), SHA256("sha256"),
		;
		@Getter
		private String value;

		EncryptVersion(String value) {
			this.value = value;
		}
	}
}
